import { actOK } from './interact'

export interface ClientKVCache {
    tabs?: string[],
}

export function nav(url) {
  if (!import.meta.env.SSR) {
    window.location.href = url
  }
}

export function navBack() {
  if (!import.meta.env.SSR) {
    window.history.go(-1)
  }
}

export function toFullScreen() {
  if (!import.meta.env.SSR) {
    document.documentElement.requestFullscreen()
  }
}

export function setToken(token: string) {
  if (!import.meta.env.SSR) {
    window.localStorage.setItem('DECISION_TOKEN', token)
  }
}

export function getToken() {
  if (!import.meta.env.SSR) {
    const paramToken = getParam('token')
    paramToken && setToken(paramToken)
    return window.localStorage.getItem('DECISION_TOKEN')
  }
}

export function setFields(data: any) {
  if (!import.meta.env.SSR) {
    window.localStorage.setItem('DECISION_FIELDS', JSON.stringify(data))
  }
}

export function queryFields() {
  if (!import.meta.env.SSR) {
    const res = window.localStorage.getItem('DECISION_FIELDS')
    return res ? JSON.parse(res) : []
  }
}

export function setEditingFlows(data: any) {
  if (!import.meta.env.SSR) {
    window.localStorage.setItem('DECISION_EDITING_FLOWS', JSON.stringify(data))
  }
}

export function queryEditingFlows() {
  if (!import.meta.env.SSR) {
    const res = window.localStorage.getItem('DECISION_EDITING_FLOWS')
    return res ? JSON.parse(res) : []
  }
}

export function setSetting(type: string, data: any) {
  if (!import.meta.env.SSR) {
    window.localStorage.setItem('DECISION_SETTING' + type, JSON.stringify(data))
  }
}

export function getSetting(type: string) {
  if (!import.meta.env.SSR) {
    const res = window.localStorage.getItem('DECISION_SETTING' + type)
    return res ? JSON.parse(res) : []
  }
}

export function setDataDetails(data: any) {
  if (!import.meta.env.SSR) {
    window.localStorage.setItem('DECISION_DATA_DETAILS', JSON.stringify(data))
  }
}

export function getDataDetails() {
  if (!import.meta.env.SSR) {
    const res = window.localStorage.getItem('DECISION_DATA_DETAILS')
    return res ? JSON.parse(res) : []
  }
}

export function clear() {
  if (!import.meta.env.SSR) {
    window.localStorage.clear()
  }
}

export function copy(content: string) {
  navigator.clipboard.writeText(content)
  actOK('已复制')
}

export function getParam(key: string): string | null {
  if (!import.meta.env.SSR) {
    const params = new URLSearchParams(window.location.search)
    const param = params.get(key)
    return param
  }
  return null
}

export function isMobile(): boolean {
  if (!import.meta.env.SSR) {
    if (document.body.clientWidth <= 900) {
      return true
    }
  }
  return false
}

export function setClientKVCache<T extends keyof ClientKVCache>(key: T, value: ClientKVCache[T]) {
  if (!import.meta.env.SSR) {
    const existed = window.localStorage.getItem('DECISION_TABS_CACHE')
    if (!existed) {
      const cache: ClientKVCache = {}
      cache[key] = value
      window.localStorage.setItem('DECISION_TABS_CACHE', JSON.stringify(cache))
      return
    }
    const cache: ClientKVCache = JSON.parse(existed)
    cache[key] = value
    window.localStorage.setItem('DECISION_TABS_CACHE', JSON.stringify(cache))
  }
}

export function getClientKVCache<T extends keyof ClientKVCache>(key: T): ClientKVCache[T] | null {
  if (!import.meta.env.SSR) {
    const existed = window.localStorage.getItem('DECISION_TABS_CACHE')
    if (!existed) return null
    const cache: ClientKVCache = JSON.parse(existed)
    return cache[key]
  }
  return null
}